/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.blacklist.dao;

import com.elcom.util.Constant;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.jconfig.Configuration;
import org.jconfig.ConfigurationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Administrator
 */
public class DbAccess {

    private static DbAccess instance;
    private static final Logger logger = LoggerFactory.getLogger(DbAccess.class);
    public static String fileName;

    private DbAccess() {
    }

    public static synchronized DbAccess getInstance() {
        if (instance == null) {
            return new DbAccess();
        } else {
            return instance;
        }
    }

    private Connection getConnection(String classDriverName, String url, String user, String pass) {
        Connection con = null;
        try {
            Class.forName(classDriverName);
            Properties properties = new Properties();
            properties.put("user", user);
            properties.put("password", pass);
            properties.put("charSet", "UTF8");
            con = DriverManager.getConnection(url, properties);
            con.setAutoCommit(false);
        } catch (ClassNotFoundException ex) {
            logger.error("", ex);
        } catch (SQLException ex) {
            logger.error("", ex);
        }
        return con;
    }

    public Connection getConnection() {
        try {
            Connection conn;

            Configuration config = ConfigurationManager.getConfiguration(Constant.SYS_CONFIG_NAME);
            String url = config.getProperty("url", "jdbc:oracle:thin:@//192.168.7.146:1521/orcl", "database");
            String username = config.getProperty("username", "livescreen", "database");
            String password = config.getProperty("password", "livescreen", "database");
            String classDriverName = config.getProperty("classDriverName", "oracle.jdbc.OracleDriver", "database");

            conn = getConnection(classDriverName, url, username, password);
            return conn;
        } catch (Exception ex) {
            logger.error("", ex);
            return null;
        }
    }

    public static void closeConnection(Connection cn) {
        if (cn != null) {
            try {
                if (!cn.isClosed()) {
                    cn.close();
                }
            } catch (SQLException ex) {
                logger.error("", ex);
            }
        }
    }

    public static void commitTransaction(Connection cn) {
        try {
            cn.commit();
        } catch (SQLException ex) {
            logger.error("", ex);
        }
    }

    public static void rollBackTransaction(Connection cn) {
        try {
            cn.rollback();
        } catch (SQLException ex) {
            logger.error("", ex);
        }
    }

    public static void closeConn(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception ex) {
                logger.error("", ex);
            }
        }
    }

    public static void closePreparedStatement(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (Exception ex) {
                logger.error("", ex);
            }
        }
    }

    public static void closeCallableStatement(CallableStatement cs) {
        if (cs != null) {
            try {
                cs.close();
            } catch (Exception ex) {
                logger.error("", ex);
            }
        }
    }

    public static void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
                logger.error("", ex);
            }
        }
    }

    public static void closeStatement(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception ex) {
                logger.error("", ex);
            }
        }
    }
}
